package org.fizz.viper.example;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;

import org.fizz.viper.ViperHTTPConnection;

/**
 * demo
 * @author FIZZ
 * @version viper-1.0, 2014-4-17
 * @since 1.0
 */
public class HTTPConnExample {

	public static void main(String[] args) throws IOException {
		//HTTPConnectionManager vtc = new HTTPConnectionManager("http://www.sina.com.cn", "127.0.0.1", 8087);
		
		ViperHTTPConnection vtc = new ViperHTTPConnection("http://www.sina.com.cn");
		
		
		InputStream is = vtc.getHTMLStream();
		ReadableByteChannel rbc = Channels.newChannel(is);
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		
		Charset charset = Charset.forName(vtc.getEncoding());
		
		while(true){
			buffer.clear();
			int k = rbc.read(buffer);
			if(k == -1){
				break;
			}
			buffer.flip();
			
			CharBuffer cb = charset.decode(buffer);
			System.out.println(cb);
		}
		
	}

}
